package com.example.administrator.energyida.adapter;

import java.util.ArrayList;
import java.util.List;
import java.util.Spliterators;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.example.administrator.energyida.R;
import com.example.administrator.energyida.beans.EnergyBean;
import com.google.zxing.common.StringUtils;

public class ItemGirdviewHomeAdapter extends BaseAdapter {

    private List<EnergyBean> objects = new ArrayList<EnergyBean>();
    private String[] colors = {"#f78127", "#10cafd", "#1afa29", "#d81e06"};
    private Context context;
    private LayoutInflater layoutInflater;
    private SetUpOrDownCallback setUpOrDownCallback;

    public ItemGirdviewHomeAdapter(Context context,List<EnergyBean> objects, SetUpOrDownCallback setUpOrDownCallback) {
        this.objects = objects;
        this.context = context;
        this.layoutInflater = LayoutInflater.from(context);
        this.setUpOrDownCallback = setUpOrDownCallback;
    }

    @Override
    public int getCount() {
        return objects.size();
    }

    @Override
    public Object getItem(int position) {
        return objects.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = layoutInflater.inflate(R.layout.item_girdview_home, null);
            convertView.setTag(new ViewHolder(convertView));
        }
        initializeViews(position, getItem(position), (ViewHolder) convertView.getTag());
        return convertView;
    }

    private void initializeViews(int pos, Object object, ViewHolder holder) {
        //TODO implement
        EnergyBean energyBean = (EnergyBean) object;
        for (int i = 0; i < objects.size(); i++) {
        }
        switch (pos) {
            case 0:
                setUpOrDownCallback.callback(true,holder.gridItemHomeTextEnergyCompare);
                holder.gridItemHomeTextEnergyValue.setTextColor(Color.parseColor("#FFF78229"));
                break;
            case 1:
                setUpOrDownCallback.callback(false,holder.gridItemHomeTextEnergyCompare);
                holder.gridItemHomeTextEnergyValue.setTextColor(Color.parseColor("#FF12CAFD"));
                break;
            case 2:
                setUpOrDownCallback.callback(true,holder.gridItemHomeTextEnergyCompare);
                holder.gridItemHomeTextEnergyValue.setTextColor(Color.parseColor("#FF66FC70"));
                break;
            case 3:
                setUpOrDownCallback.callback(true,holder.gridItemHomeTextEnergyCompare);
                holder.gridItemHomeTextEnergyValue.setTextColor(Color.parseColor("#FFD82008"));
                break;
            default:
                break;
        }
        //调用setCompoundDrawables时，必须调用Drawable.setBounds()方法,否则图片不显示
        energyBean.getDrawable().setBounds(0, 0, energyBean.getDrawable().getMinimumWidth(), energyBean.getDrawable().getMinimumHeight());
        holder.gridItemHomeTextEnergyName.setCompoundDrawables(energyBean.getDrawable(), null, null, null);
        holder.gridItemHomeTextEnergyName.setText(energyBean.getName());
        holder.gridItemHomeTextEnergyValue.setText(energyBean.getValue());
        holder.gridItemHomeTextEnergyCompare.setText(energyBean.getCompare());
        holder.gridItemHomeTextEnergyLast.setText(energyBean.getLast());
    }

    protected class ViewHolder {
        private TextView gridItemHomeTextEnergyName;
        private TextView gridItemHomeTextEnergyValue;
        private TextView gridItemHomeTextEnergyCompare;
        private TextView gridItemHomeTextEnergyLast;

        public ViewHolder(View view) {
            gridItemHomeTextEnergyName = view.findViewById(R.id.gridItem_home_text_energyName);
            gridItemHomeTextEnergyValue = view.findViewById(R.id.gridItem_home_text_energyValue);
            gridItemHomeTextEnergyCompare = view.findViewById(R.id.gridItem_home_text_energyCompare);
            gridItemHomeTextEnergyLast = view.findViewById(R.id.gridItem_home_text_energyLast);
        }
    }

    public interface SetUpOrDownCallback{
        void callback(boolean b, TextView textView);
    }

}
